package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-17
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //查询所有数据
    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("items", list);
    }

    //逻辑删除数据
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    @CacheEvict(value = "teacher", allEntries = true)
    public R removeTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //分页查询
    @ApiOperation("分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name = "current", value = "当前页数", required = true) @PathVariable long current,
                             @ApiParam(name = "limit", value = "每页数量", required = true) @PathVariable long limit){
        try {
            int i = 10/0;
        }catch (Exception e){
            //抛出自定义异常
            throw new GuliException(20001,"执行了hzml的自定义异常处理。。");
        }
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //实现分页
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();    //总记录数
        List<EduTeacher> records = pageTeacher.getRecords();    //数据集合

        return R.ok().data("total",total).data("rows",records);
    }

    //条件查询带分页,有RequestBody必须用post提交
    @ApiOperation("条件查询带分页查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页数", required = true) @PathVariable long current,
                                  @ApiParam(name = "limit", value = "每页数量", required = true) @PathVariable long limit,
                                  @ApiParam(name = "teacherQuery", value = "查询条件") @RequestBody TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //判断值是否为空，不为空拼条件
        String name = teacherQuery.getName();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        if(!StringUtils.isEmpty(name)){
           wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_modified");

        //实现分页
        eduTeacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();    //总记录数
        List<EduTeacher> records = pageTeacher.getRecords();    //数据集合

        return R.ok().data("total",total).data("rows",records);
    }

    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    @CacheEvict(value = "teacher", allEntries = true)
    public R addTeacher(@ApiParam(name = "eduTeacher", value = "讲师信息") @RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据id查询讲师
    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable("id") String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    //修改讲师
    @ApiOperation("根据id查询讲师")
    @PostMapping("updateTeacher")
    @CacheEvict(value = "teacher", allEntries = true)
    public R updateTeacher(@ApiParam(name = "eduTeacher", value = "讲师信息") @RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

